package com.six.core.controller;

import com.six.core.entity.Emp;
import com.six.core.mapper.EmpMapper;
import com.six.core.service.EmpService;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.EmpVo;

import com.six.core.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description:
 * @author:LiWei
 * @date:2023年02月27日10:10
 */

@Api(value = "/emp", tags = {":"})
@RestController
@RequestMapping("/emp/emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private EmpMapper empMapper;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "currentPage", value = "", required = false),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "pageSize", value = "", required = false),
            @ApiImplicitParam(paramType = "body", dataType = "EmpVo", name = "empVo", value = "", required = false)
    })
    @ApiOperation(value = "", notes = "")

    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result list(@PathVariable Integer currentPage, @PathVariable Integer pageSize,  @RequestBody EmpVo empVo){
        return empService.findAll(currentPage, pageSize, empVo);


    }
    //删除
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping("/deleteById/{id}")
    public Result deleteById (@PathVariable Integer id){
        return  empService.deleteByEmpId(id);

    }

    //添加
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Emp", name = "emp", value = "", required = true)
    })
    @ApiOperation(value = "添加", notes = "添加")
    @RequestMapping("/addEmp")
    public Result addEmp(@RequestBody Emp emp){
        return empService.addEmp(emp);
    }

    //修改
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Emp", name = "emp", value = "", required = true)
    })
    @ApiOperation(value = "修改", notes = "修改")
    @RequestMapping("/updateEmp")
    public Result updateEmp(@RequestBody Emp emp){
        return empService.updateEmp(emp);

    }

    //修改密码
    @RequestMapping("/updatePassword")
    public Result updatePasswrod(@RequestBody EmpVo empVo, HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer empId = (Integer) map.get("empId");
        Emp emp1 = empMapper.selectById(empId);
//        String password = (String) map.get("password");
//        String oldPassword = empVo.getOldPassword();
//        String newPassword = empVo.getNewPassword();
        Emp emp = empService.getById(empId);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean matches = encoder.matches(empVo.getOldPassword(), emp1.getPassword());
        if (!matches){
            return new Result(500,"输入旧密码错误");
        }
        emp.setPassword(encoder.encode(empVo.getNewPassword()));
        empService.updateById(emp);
        return new Result(200,"修改密码成功");

    }


    //修改用户状态
    @RequestMapping("/updateDisable/{id}/{deleted}")
    public Result updateDisable(@PathVariable Integer id,@PathVariable Integer deleted){
        Emp emp=new Emp();
        emp.setIsDisable(deleted);
        emp.setId(id);
        return  empService.updateDisable(emp);
    }


    @PostMapping("/loginOut")
    public Result loginOut(){
        String token = WebUtil.getRequest().getHeader("token");
        redisTemplate.delete(token);
        return new Result(200,"退出成功");
    }


}














